<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #wanho{
            width: 100px;
            height: 100px;
            background: pink;
        }
    </style>
    <script>
        function f1(){
            console.log(111)
        }

        // 当页面加载完成后再执行
        window.onload = function(){
            var hello = document.getElementById('hello')
            hello.onclick = function(event){ // event表示事件对象，包含事件的相关信息
                console.log(222)
                console.log(event)
                console.log(event.target) // 事件源
                console.log(this) // this表示事件源
            }
        }

    </script>
</head>
<body>
    <!-- 方式1：静态绑定 -->
    <button onclick="f1()">按钮1</button>

    <!-- 方式2：动态绑定 -->
    <button id="hello">按钮2</button>

    <div id="wanho">万和</div>


    <form action="" method="get" id="myForm">
        用户名：<input type="text" id="username" name="username"> <br>
        全选：<input type="checkbox" id="all"> <br>
        <input type="submit" value="提交">
    </form>


    <script>
        // 鼠标事件
        let wanho = document.getElementById('wanho')
        wanho.onclick = function(){
            console.log(this.innerText)
        }

        wanho.ondblclick = function(){
            console.log(222)
        }

        wanho.onmouseover = function(){
            console.log(333)
        }

        wanho.onmouseout = function(){
            console.log(444)
        }


        // 键盘事件
        var username = document.getElementById('username')
        username.onkeydown = function(){
            // console.log('down')
        }
        username.onkeypress = function(){
            // console.log('press')
        }
        username.onkeyup = function(e){
            // console.log('up')
            if(e.keyCode == 13){
                console.log(this.value)
            }
        }

        // 表单事件
        username.onfocus = function(){
            this.style.background='pink'
        }
        username.onblur = function(){
            this.style.background='white'
        }

        document.getElementById('all').onchange = function(){
            console.log(this.checked ? 1 : 2)
        }

        document.getElementById('myForm').onsubmit = function(){
            alert('用户想要提交表单。。。。。。')

            return false // false表示阻止提交
        }

    </script>
</body>
</html>